package hot_100;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class 组合总和 {
    public static void main(String[] args) {

    }

    List<List<Integer>> ans = new LinkedList<>();
    Deque<Integer> list = new LinkedList<>();
    int[] numbs;
    int n;
    int tar;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        n = candidates.length;
        tar = target;
        Arrays.sort(candidates);
        numbs = candidates;
        Dfs(0,0);
        return ans;
    }

    public void Dfs(int x, int sum) {
        if (sum > tar)
            return;
        if (sum == tar) {
            ans.add(new LinkedList<>(list));
            return;
        }
        for (int i = x; i < n; i++) {
            list.addLast(numbs[i]);
            Dfs(i, sum + numbs[i]);
            list.removeLast();
        }
    }

}
